package com.gxc.utils;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 定义rest接口的统一返回值. 之前是在了map中，结构化不好，无法共用.
 *
 * @author 王靖靖
 * @date 25/02/2018 11:35
 */
public class RestControllerResult<T> implements Serializable {

  private Boolean success;
  private Integer code;

  private List<String> infoMsgs = new LinkedList<>();
  private List<String> warningMsgs = new LinkedList<>();
  private String errorMsg;
  private T data;

  public RestControllerResult() {
    super();
  }

  public RestControllerResult(T t) {
    super();
    this.data = t;
  }

  public Boolean getSuccess() {
    return success;
  }

  public void setSuccess(Boolean success) {
    this.success = success;
  }

  public Integer getCode() {
    return code;
  }

  public void setCode(int code) {
    this.code = code;
  }

  public List<String> getInfoMsgs() {
    return infoMsgs;
  }

  public void setInfoMsgs(List<String> infoMsgs) {
    this.infoMsgs = infoMsgs;
  }

  public List<String> getWarningMsgs() {
    return warningMsgs;
  }

  public void setWarningMsgs(List<String> warningMsgs) {
    this.warningMsgs = warningMsgs;
  }

  public String getErrorMsg() {
    return errorMsg;
  }

  public void setErrorMsg(String errorMsg) {
    this.errorMsg = errorMsg;
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  /**
   * 创建成功的RestControllerResult对象.
   *
   * @param data 返回的数据
   * @return 包含成功标识的RestControllerResult对象
   */
  public static <T> RestControllerResult<T> success(T data) {
    RestControllerResult result = new RestControllerResult<>(data);
    result.setSuccess(true);
    return result;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    RestControllerResult<?> that = (RestControllerResult<?>) o;
    return code == that.code
        && Objects.equals(success, that.success)
        && Objects.equals(infoMsgs, that.infoMsgs)
        && Objects.equals(warningMsgs, that.warningMsgs)
        && Objects.equals(errorMsg, that.errorMsg)
        && Objects.equals(data, that.data);
  }

  @Override
  public int hashCode() {

    return Objects.hash(success, code, infoMsgs, warningMsgs, errorMsg, data);
  }

  @Override
  public String toString() {
    final StringBuffer sb = new StringBuffer("RestControllerResult{");
    sb.append("success=").append(success);
    sb.append(", code=").append(code);
    sb.append(", infoMsgs=").append(infoMsgs);
    sb.append(", warningMsgs=").append(warningMsgs);
    sb.append(", errorMsg='").append(errorMsg).append('\'');
    sb.append(", data=").append(data);
    sb.append('}');
    return sb.toString();
  }
}
